package cn.edu.ujn.ch10.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.ujn.ch10.dao.Customer;
import cn.edu.ujn.ch10.dao.CustomerMapper;
@Service
public class CustomerServiceImpl implements ICustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	@Override
	public Customer addCustomer(Customer customer) {
		int insert = this.customerMapper.insert(customer);
		return customer;
	}

	@Override
	public Customer findCustomerById(int id) {
		// TODO Auto-generated method stub
		Customer customer = customerMapper.selectByPrimaryKey(id);
		return customer;
	}

	@Override
	public List<Customer> findAllCustomers() {
		// TODO Auto-generated method stub
	 List<Customer> customers=customerMapper.selectAll();
	return customers;
	}

	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		if ((customerMapper.deleteByPrimaryKey(id))==1) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean updateCustomer(Customer customer) {
		Customer initCustomer = customerMapper.selectByPrimaryKey(customer.getId());
		System.out.println("the old cord:"+initCustomer);
		initCustomer.setUsername(customer.getUsername());
		initCustomer.setJobs(customer.getJobs());
		initCustomer.setPhone(customer.getPhone());
		int updateByPrimaryKey = customerMapper.updateByPrimaryKey(initCustomer);
		if(updateByPrimaryKey==1) {
		return true;
		}else {
			return false;
		}
	}

}
